﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Surveying;
using Point = Surveying.Point;

namespace GIS_SpatialAnalyst
{
    class Point_PolygonClass
    {
        public bool Is_Point_in_Polygon(Polygon mpolygon,Point p)
        {
            bool retBL = false;
            Point pE1 = new Point(p.x + 10000, p.y);
            Point pE2 = new Point(p.x - 10000, p.y);
            int pj_count = 0;
            for (int i = 0; i <mpolygon.points.Length; i++)
            {
                Point p1, p2;
                if (i == mpolygon.points.Length-1)
                {
                    p1 = mpolygon.points[i];
                    p2 = mpolygon.points[1];
                }
                else
                {
                    p1 = mpolygon.points[i];
                    p2 = mpolygon.points[i+1];
                }
                if ((p1.y>=p.y&&p2.y<=p.y))
                {
                    double d1 = direction(p1,p2,p);
                    double d2 = direction(p1,p2,pE1);
                    double d3 = direction(p,pE1,p1);
                    double d4 = direction(p,pE1,p2);
                  
                    if ((d1 * d2 < 0 && d3 * d4 < 0))
                    {
                        pj_count++;
                    }
                }
               
            }
            if (pj_count%2==0)
            {
                retBL = false;
            }
            else
            {
                retBL = true;
            }
            return retBL;
        }

        public double direction(Point p1, Point p2, Point p3)
        {
                Point d1 = new Point(p3.x - p1.x, p3.y - p1.y);
                Point d2 = new Point(p2.x - p1.x, p2.y - p1.y);
                return d1.x * d2.y - d1.y * d2.x;
        }
        public static double  new_direction(Point p1, Point p2, Point p3)
        {
            Point d1 = new Point(p3.x - p1.x, p3.y - p1.y);
            Point d2 = new Point(p2.x - p1.x, p2.y - p1.y);
            return d1.x * d2.y - d1.y * d2.x;
        }
        public static bool PointInFences(Point p, Polygon polygon)
        {
            bool retBL = false;
            Point pE1 = new Point(p.x + 10000, p.y);
            int pj_count = 0;
            for (int i = 0; i < polygon.points.Length; i++)
            {
                Point p1, p2;
                if (i == polygon.points.Length - 1)
                {
                    p1 = polygon.points[i];
                    p2 = polygon.points[0];
                }
                else
                {
                    p1 = polygon.points[i];
                    p2 = polygon.points[i + 1];
                }

                if ((p1.y >= p.y && p2.y <= p.y) || (p1.y <= p.y && p2.y >= p.y))
                {
                    double d1 = new_direction(p1, p2, p);
                    if (d1< 1E-2 && d1> -1E-2)
                    {
                        return true;
                    }
                    double d2 = new_direction(p1, p2, pE1);
                    double d3 = new_direction(p, pE1, p1);
                    double d4 = new_direction(p, pE1, p2);
                    if ((d1 * d2 <0 && d3 * d4 <0))
                    {
                        pj_count++;
                    }
                }

            }
            if (pj_count % 2 == 0)
            {
                retBL = false;
            }
            else
            {
                retBL = true;
            }
            return retBL;

        }
    }
}
